#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QFont.h>
#include <QTimer>
#include <QKeyEvent>
int i=0;bool over=0;int score=0,ex[7][7]={0};
int a[7]={7,7,7,7,7,7,7};int stop=0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),ifrun(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(500,50,1300,1020));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!ifrun)
    Initnumber();
    //画背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(30,30,840,960);
    painter.setPen(Qt::blue);
    for(int i=1;i<=6;i++){
        painter.drawLine(30,150,870,150);
        painter.drawLine(30,150+i*120,870,150+i*120);
        painter.drawLine(30+i*120,30,30+i*120,990);
    }
    //画方格
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(900,45,d1);
painter.drawText(900,75,d2);painter.drawText(900,105,d3)
        ;painter.drawText(900,135,d4);painter.drawText(900,165,d5);
painter.drawText(960,165,QString::number(score));
painter.drawText(960,250,d7);
painter.drawText(960,350,d8);
painter.drawText(960,450,QString::number(a[0]));
painter.drawText(960,550,QString::number(a[1]));
painter.drawText(960,650,QString::number(a[2]));
painter.drawText(960,750,QString::number(a[3]));
painter.drawText(960,850,QString::number(a[4]));
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    for(int k=0;k<=i;k++){
        switch (num[k]) {
        case 0:
             painter.drawPixmap(number[k],QPixmap(":/lhy/ima/1.bmp"));
            break;
        case 1:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/2.bmp"));
           break;
        case 2:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/4.bmp"));
           break;
        case 3:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/8.bmp"));
           break;
        case 4:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/16.bmp"));
           break;
        case 5:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/32.bmp"));
           break;
        case 6:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/64.bmp"));
           break;
        case 7:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/128.bmp"));
           break;
        case 8:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/256.bmp"));
           break;
        case 9:
            painter.drawPixmap(number[k],QPixmap(":/lhy/ima/512.bmp"));
           break;
        default:
            break;
        }


    }
    //painter.drawRects(&number[0],number.size());
    if(stop==1){
        painter.drawText(900,200,d9);
    }
    if(over){
        timer->stop();
        timer2->stop();
        painter.drawText(900,200,d6);
    }
}
void MainWindow::Initnumber(){
    place=3;
    ifrun=true;
    d1="游戏规则：使用键盘";
    d2="“左”“右”“下”三个按键，";
    d3="控制数字的下落，";
    d4= "使相同的数字相遇";
    d5="得分：";
    d6="游戏结束";
    d7="（按“s”暂停，按“c”继续）";
    d8="made by 刘恒宇";
    d9="游戏暂停";
    //QRect rect(390,30,120,120);
    //number=rect;
    number.resize(200);
    num.resize(200);
    x.resize(200);
    y.resize(200);
    for(int p=0;p<number.size();p++){
        QRect rect(390,30,120,120);
        number[p]=rect;x[p]=3,y[p]=0;
        num[p]=p*p%7;
    }//初始化数字
    timer=new QTimer(this);//设定计时器
    timer2=new QTimer(this);
    timer->start(500);//时间500ms
    timer2->start(100);//100ms
    connect(timer,SIGNAL(timeout()),SLOT(num_fall()));
    connect(timer2,SIGNAL(timeout()),SLOT(num_move()));
}void MainWindow::num_move(){
    
if(stop==0){

   switch (direct) {
    case 1:
        if(place>0&&y[i]<a[x[i]-1]){
            x[i]--;
        number[i].setLeft(number[i].left()-120);
        number[i].setRight(number[i].right()-120);
        place--;}
        break;
    case 2:
        if(place<6&&y[i]<a[x[i]+1]){
            x[i]++;
        number[i].setLeft(number[i].left()+120);
        number[i].setRight(number[i].right()+120);
        place++;}
        break;
   case 3:
       if(y[i]<a[place]-1){
           y[i]++;
           number[i].setTop(number[i].top()+120);
           number[i].setBottom(number[i].bottom()+120);
       break;}
    default:
        break;
    }
    direct=4;}
    update();

}
void MainWindow::num_fall(){
if(stop==0){
    if(y[i]>=a[place]){
        a[place]--;
        for(int w=i;w>0;w--){
                for(int e=0;e<i;e++){
                    //if(num[w]==num[i]&&(y[w]==y[i]&&(x[w]-x[i]==1||x[i]-x[w]==1)||(x[w]==x[i]&&(y[w]-y[i]==-1||y[w]-y[i]==1)))){
                    if(e!=w&&num[e]==num[w])
                        if((x[e]==x[w]&&y[e]-y[w]==1)||(y[e]==y[w]&&(x[e]-x[w]==1||x[w]-x[e]==1)))
                                {
                  
                                    num[e]++;for(int ii=i;ii>=0;ii--)if(x[ii]==x[w]&&y[ii]<y[w]){
									 y[ii]++;
    number[ii].setTop(number[ii].top()+120);
    number[ii].setBottom(number[ii].bottom()+120);
                                    }

                        num[w]=-w;



                        score++;
                    a[x[w]]++;


                        }
                }
            }


        i++;place=3;if(a[place]<1)over=1;
        update();}
    y[i]++;
    number[i].setTop(number[i].top()+120);
    number[i].setBottom(number[i].bottom()+120);}

    update();
}
void MainWindow::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*) event;
    switch (key->key()){
    case Qt::Key_Left: direct=1;
        break;
    case Qt::Key_Right: direct=2;
        break;
    case Qt::Key_Down: direct=3;break;
case Qt::Key_S: stop=1;break;
    case Qt::Key_C: stop=0;break;
    default:
        break;
    }
}
